//
//  CustomTabBarController.m
//  HuateVideo
//
//  Created by Tiger on 12-5-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CustomTabBarController.h"
#define ICON_WIDTH 24

@interface CustomTabBarController ()

@end

@implementation CustomTabBarController

- (id)initWithArray:(NSArray *)array
{
    self = [super init];
    if (self) {
        // Custom initialization
        
        itemArray = [[NSArray alloc]initWithArray:array]; 
        self.tabBar.hidden = YES;
        
        tabView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 431, 320, 49)];
        tabView.tag=110;
        tabView.image = [UIImage imageNamed:@"bot-bg.png"];
        tabView.userInteractionEnabled = YES;
        [self.view addSubview:tabView];
        [self.view bringSubviewToFront:tabView];
    }
    return self;
}

- (void)setSelectedItemIndex:(NSInteger)index
{
    self.selectedIndex = index;
    
    if (itemArray) {
        itemWidth = 320.0/[itemArray count];
        
        if (!selectedImage) {
            selectedImage = [[UIImageView alloc]init];
            [tabView addSubview:selectedImage];
        }
        
        selectedImage.frame = CGRectMake(itemWidth*index, 0, itemWidth, tabView.frame.size.height);
        selectedImage.image = [UIImage imageNamed:@"bot-bg-horver.png"];
        
        for (int i=0; i<[itemArray count]; i++) {
            UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
            item.frame = CGRectMake(itemWidth*i, 0, itemWidth, tabView.frame.size.height);
            item.tag = 1000+i;
            [item addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchDown];
            [tabView addSubview:item];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, item.frame.size.height-20, itemWidth, 20)];
            label.text = LS([[itemArray objectAtIndex:i] objectForKey:@"title"]);
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:12.0];
            label.textColor = [UIColor whiteColor];
            label.textAlignment = UITextAlignmentCenter;
            [item addSubview:label];
            [label release];
            
            UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake((itemWidth-ICON_WIDTH)/2, 5, ICON_WIDTH, ICON_WIDTH)];
            icon.image = [UIImage imageNamed:[[itemArray objectAtIndex:i] objectForKey:@"icon"]];
            [item addSubview:icon];
            [icon release];
        }
    }
}


- (void)dissmissTabBarWithAnimate{
    //NSLog(@"%@",self.view.subviews);
    
    //BOOL shouldDelete = NO;
    for (UIView* o in self.view.subviews) {
        
//        if (!shouldDelete && [o isKindOfClass:NSClassFromString(@"UIImageView")]) {
//            shouldDelete = YES;
//            continue;
//        }
//        else if (shouldDelete && [o isKindOfClass:NSClassFromString(@"UIImageView")]){
//            [o removeFromSuperview];
//            continue;
//        }
        
        if ([o isKindOfClass:NSClassFromString(@"UITransitionView")]) {
            o.frame=CGRectMake(0, 0, 320, 480); 
        }
    }
    
    //UIImageView* tabImg =(UIImageView*) [self.view viewWithTag:110];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.3f];
    [tabView setFrame:CGRectMake(0, 431+49, 320, 49)];
    [UIView commitAnimations];
}
- (void)ShowTabBarWithAnimate{
    //NSLog(@"%@",self.view.subviews);
    for (UIView* o in self.view.subviews) {
        if ([o isKindOfClass:NSClassFromString(@"UITransitionView")]) {
            o.frame=CGRectMake(0, 0, 320, 431); 
        }
    }
    //UIImageView* tabImg =(UIImageView*) [self.view viewWithTag:110];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.3f];
    [tabView setFrame:CGRectMake(0, 431, 320, 49)];
    [UIView commitAnimations];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)dealloc
{
    [selectedImage release];
    [itemArray release];
    [tabView release];
    [super dealloc];
}

- (void)selectedItem:(UIButton *)sender
{
    if (sender.tag - 1000 == 3) {
        NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//[NSArray arrayWithObject:@"public.movie"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:START_CAMERA_NOTIFICATION object: mediaTypes];
    }
    else {
        
        if (self.selectedIndex == sender.tag - 1000) {
            [[self.viewControllers objectAtIndex:self.selectedIndex] popToRootViewControllerAnimated:YES];
        }
        else {
            self.selectedIndex = sender.tag - 1000;
            CGRect frame = selectedImage.frame;
            selectedImage.frame = CGRectMake(itemWidth*self.selectedIndex, 0, itemWidth, frame.size.height);
        }
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
